import React, {useEffect, useState} from "react";
import Taro, {getCurrentInstance} from "@tarojs/taro";
import {View} from "@tarojs/components";
import {orderDetailServer} from "@/services/order";
import Takeout from "./takeout";
import Product from "./product";
import Location from "./location";

import './index.scss'



const SubmitCommentPage = () => {

  //订单信息
  const [state,updateState]=useState({})

  const setState=(val)=>{
    const res={...state,...val}
    updateState(res)
  }

  //初始化
  useEffect(() => {
    if(!state.id){
      if(getCurrentInstance().router.params.appId=='thjs_locallife_org'){
        setState({
          id:1,
          type:0,
          storeId:getCurrentInstance().router.params.id,
          storeName:decodeURIComponent(getCurrentInstance().router.params.name)
        })
      }else{
        orderDetailServer(getCurrentInstance().router.params.id).then(res=>{
          if(res.code!=='1'){
            Taro.showToast({title:'无效的订单',icon:'none'})
            Taro.navigateBack()
          }else{
            setState(res.content)
          }
        })
      }
    }
  }, [state])

  const childPage=()=>{
    switch (state.type){
      case 1://商城订单
        return (
          <Product order={state}></Product>
        )
      case 4://外卖订单
        return (
          <Takeout order={state}></Takeout>
        )
      case undefined://加载中
        return (
          <View></View>
        )
      default://本地生活
        return (
          <Location order={state}></Location>
        )
    }
  }

  return childPage()
}

export default SubmitCommentPage;
